package cn.fl.risknew.service.impl;

import cn.cmm.base.framework.exception.BaseException;
import cn.fl.arbitration.utils.PropKit;
import cn.fl.commons.result.ResponseData;
import cn.fl.overdue.service.IOverdueApiService;
import cn.fl.risknew.controller.RiskOverdueCollectController;
import cn.fl.risknew.service.IRiskLevelService;
import cn.fl.risknew.vo.CCardResult;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import rc.ccard.facade.facade.ccard.condition.RcCkCollectionQC;
import rc.ccard.facade.facade.ccard.dto.RcCkCollectionResultDTO;
import rc.gateway.facade.gateway.api.base.IBaseRcGatewayServiceApiService;
import rc.gateway.facade.gateway.dto.RcGatewayServiceSergwrDTO;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yhuiyun
 * @version 1.0.0
 * @description
 * @date 2021/3/17
 */
@Service
public class IRiskLevelServiceImpl implements IRiskLevelService {

    private static final Logger logger = LoggerFactory.getLogger(IRiskLevelServiceImpl.class);
    @Resource
    private IBaseRcGatewayServiceApiService rcGatewayServiceApiService;

    @Resource
    private IOverdueApiService overdueApiService;

    @Override
    public CCardResult getCRiskLevel(Long cstId)   throws Exception{
        //查询当前客户所有逾期项⽬ID集合
        List<Long> overDueProjectIds = overdueApiService.getOverDueProjectIds(cstId);
        List<RcCkCollectionQC> qcs = new ArrayList<>();
        RcCkCollectionQC qc = new RcCkCollectionQC();
        qc.setCstId(cstId);
        qc.setPrjIds(overDueProjectIds);
        qcs.add(qc);
        RcGatewayServiceSergwrDTO dto = new RcGatewayServiceSergwrDTO();

        dto.setPid(PropKit.use("properties/ccard/ccard.properties").get("risk.c.card.pid"));// 必填参数pid
        dto.setBid(PropKit.use("properties/ccard/ccard.properties").get("risk.c.card.bid"));// 必填参数bid
        dto.setMethodCode(PropKit.use("properties/ccard/ccard.properties").get("risk.c.card.method.code"));// 必填参数methodCode
        List<Object> params = new ArrayList<>();//业务参数放入List<Object>
        params.add(qcs);// 将构建的业务参数放入此
        dto.setParamters(params);
        logger.info("查询C卡模型入参 dto={}", JSON.toJSON(dto));
        ResponseData<List<RcCkCollectionResultDTO>> execute =rcGatewayServiceApiService.execute(dto);
        logger.info("查询C卡模型返回数据 execute={}", JSON.toJSON(execute));
        if (execute.isSuccess() && execute.getData()!=null && !execute.getData().isEmpty()) {
            RcCkCollectionResultDTO data = execute.getData().get(0);
            CCardResult result = new CCardResult();
            BeanUtils.copyProperties(data,result);
            return result;
        } else {
            throw new BaseException("查询C卡数据异常");
        }
    }

}
